package com.siwei.mes.service.system.impl;

import com.siwei.mes.common.util.DateHelper;
import com.siwei.mes.entity.system.Serial;
import com.siwei.mes.enums.GenerateCodeEnum;
import com.siwei.mes.mapper.system.SerialMapper;
import com.siwei.mes.service.system.SerialService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * (Serial)表服务实现类
 *
 * @author linzi
 * @since 2023-04-08 20:16:24
 */
@Slf4j
@Service
public class SerialServiceImpl implements SerialService {
    @Resource
    private SerialMapper serialMapper;

    @Override
    public String genNo(String code) {
        try {
            String year = DateHelper.getYear() < 10 ? "0" + DateHelper.getYear() : DateHelper.getYear().toString();
            String month = DateHelper.getMonth() < 10 ? "0" + DateHelper.getMonth() : DateHelper.getMonth().toString();
            String day = DateHelper.getDay() < 10 ? "0" + DateHelper.getDay() : DateHelper.getDay().toString();
            Serial serial = serialMapper.selectBySerialNo(code);
            GenerateCodeEnum generateCodeEnum = GenerateCodeEnum.getName(code);
            String applyIdNumber = String.format(year.substring(2, 4) + month + day + "%03d", 0);
            if (code.equals(GenerateCodeEnum.EXPERIMENT.getCode())) {
                applyIdNumber = String.format(year.substring(2, 4) + month + day + "%05d", 0);
            }
            if (serial == null) {
                applyIdNumber = String.valueOf(this.getLongValue(applyIdNumber) + 1);
                serial = new Serial().setSerialNo(generateCodeEnum.getCode())
                        .setSerialNumber(applyIdNumber)
                        .setRemark(generateCodeEnum.getName());
                serialMapper.insertSelective(serial);
            } else {
                applyIdNumber = serial.getSerialNumber();
                long number = this.getLongValue(applyIdNumber);
                applyIdNumber = String.valueOf(number + 1);
                serialMapper.updateByPrimaryKeySelective(serial.setSerialNumber(applyIdNumber).setRemark(generateCodeEnum.getName()));
            }
            return code + applyIdNumber;
        } catch (Exception e) {
            log.error("genNo生成编号失败，错误信息：【{}】", e.getMessage(), e);
            return null;
        }
    }

    @Override
    public String genNoNumber(String code, String formatStr) {
        try {
            Serial serial = serialMapper.selectBySerialNo(code);
            GenerateCodeEnum generateCodeEnum = GenerateCodeEnum.getName(code);
            String applyIdNumber = String.format(formatStr, 0);
            if (serial == null) {
                applyIdNumber = String.format(formatStr, this.getLongValue(applyIdNumber) + 1);
                serial = new Serial().setSerialNo(generateCodeEnum.getCode())
                        .setSerialNumber(applyIdNumber)
                        .setRemark(generateCodeEnum.getName());
                serialMapper.insertSelective(serial);
            } else {
                applyIdNumber = serial.getSerialNumber();
                long number = this.getLongValue(applyIdNumber);
                applyIdNumber = String.format(formatStr, number + 1);
                serialMapper.updateByPrimaryKeySelective(serial.setSerialNumber(applyIdNumber).setRemark(generateCodeEnum.getName()));
            }
            return applyIdNumber;
        } catch (Exception e) {
            log.error("genNoNumber生成编号失败，错误信息：【{}】", e.getMessage(), e);
            return null;
        }
    }

    @Override
    public String genNoNumberByYear(String code, String formatStr) {
        try {
            Integer year = DateHelper.getYear() % 100;
            Serial serial = serialMapper.selectBySerialNo(code);
            GenerateCodeEnum generateCodeEnum = GenerateCodeEnum.getName(code);
            String applyIdNumber = year + String.format(formatStr, 0);
            if (serial == null) {
                applyIdNumber = String.format(formatStr, this.getLongValue(applyIdNumber) + 1);
                serial = new Serial().setSerialNo(generateCodeEnum.getCode())
                        .setSerialNumber(applyIdNumber)
                        .setRemark(generateCodeEnum.getName());
                serialMapper.insertSelective(serial);
            } else {
                applyIdNumber = serial.getSerialNumber();
                long number = this.getLongValue(applyIdNumber);
                applyIdNumber = String.format(formatStr, number + 1);
                serialMapper.updateByPrimaryKeySelective(serial.setSerialNumber(applyIdNumber).setRemark(generateCodeEnum.getName()));
            }
            return applyIdNumber;
        } catch (Exception e) {
            log.error("genNoNumberByYear生成编号失败，错误信息：【{}】", e.getMessage(), e);
            return null;
        }
    }

    @Override
    public String genNoNumberByYearStr(String code, String formatStr, String str) {
        try {
            Integer year = DateHelper.getYear() % 100;
            Serial serial = serialMapper.selectBySerialNo(code);
            GenerateCodeEnum generateCodeEnum = GenerateCodeEnum.getName(code);
            String applyIdNumber = year + str + String.format(formatStr, 0);
            if (serial == null) {
                applyIdNumber = String.format(formatStr, this.getLongValue(applyIdNumber) + 1);
                serial = new Serial().setSerialNo(generateCodeEnum.getCode())
                        .setSerialNumber(applyIdNumber)
                        .setRemark(generateCodeEnum.getName());
                serialMapper.insertSelective(serial);
            } else {
                applyIdNumber = serial.getSerialNumber();
                long number = this.getLongValue(applyIdNumber);
                applyIdNumber = String.format(formatStr, number + 1);
                serialMapper.updateByPrimaryKeySelective(serial.setSerialNumber(applyIdNumber).setRemark(generateCodeEnum.getName()));
            }
            return applyIdNumber;
        } catch (Exception e) {
            log.error("genNoNumberByYearStr生成编号失败，错误信息：【{}】", e.getMessage(), e);
            return null;
        }
    }

    @Override
    public String genNoNumberToYearStr(String code, String formatStr, String str) {
        try {
            Integer year = DateHelper.getYear() % 100;
            Serial serial = serialMapper.selectBySerialNo(code);
            GenerateCodeEnum generateCodeEnum = GenerateCodeEnum.getName(code);
            String applyIdNumber = String.format(formatStr, 0);
            if (serial == null) {
                applyIdNumber = String.format(formatStr, this.getLongValue(applyIdNumber) + 1);
                serial = new Serial().setSerialNo(generateCodeEnum.getCode())
                        .setSerialNumber(applyIdNumber)
                        .setRemark(generateCodeEnum.getName());
                serialMapper.insertSelective(serial);
            } else {
                applyIdNumber = serial.getSerialNumber();
                long number = this.getLongValue(applyIdNumber);
                applyIdNumber = String.format(formatStr, number + 1);
                serialMapper.updateByPrimaryKeySelective(serial.setSerialNumber(applyIdNumber).setRemark(generateCodeEnum.getName()));
            }
            return year + str + applyIdNumber;
        } catch (Exception e) {
            log.error("genNoNumberToYearStr生成编号失败，错误信息：【{}】", e.getMessage(), e);
            return null;
        }
    }

    /**
     * 获取long 只取数字
     *
     * @param str
     * @return
     */
    public Long getLongValue(String str) {
        try {
            Pattern pattern = Pattern.compile("\\d+");
            Matcher matcher = pattern.matcher(str);
            if (matcher.find()) {
                String numbers = matcher.group();
                return Long.valueOf(numbers);
            }
            return null;
        } catch (Exception e) {
            log.error("【{}】转换成long类型出错，错误信息：【{}】", str, e.getMessage(), e);
            return null;
        }
    }
}
